//
//  DetailTreatmentsViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/25.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "DetailTreatmentsViewController.h"
#import "AppUtils.h"
#import "UIImageView+AFNetworking.h"
#import "MedicineTableViewCell.h"
#import "QuestionDetailController.h"
#import "QuestionOnlineDetailController.h"
#import "QuestionTelDetailController.h"
#import "ClinicDetailViewController.h"

@interface DetailTreatmentsViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    __weak IBOutlet UILabel *_customerAge;
    
    __weak IBOutlet NSLayoutConstraint *backviewHeight;

    __weak IBOutlet NSLayoutConstraint *medicinetvHeight;
    __weak IBOutlet UITableView *_medicineTv;
    __weak IBOutlet UIScrollView *_dtScv;
    __weak IBOutlet UILabel *_customerSex;
    __weak IBOutlet UILabel *_customerName;
    __weak IBOutlet UIView *_iconView;
    __weak IBOutlet UIImageView *_iconImageVIew;
    __weak IBOutlet UILabel *_diagnosis;
    __weak IBOutlet UILabel *_medicinel;
    __weak IBOutlet UILabel *_remarks;
  
    NSArray *_dataAry;
}
@end

@implementation DetailTreatmentsViewController

- (void)goDetail {
    if ([_type intValue] == 0) {
        [self performSegueWithIdentifier:@"toYiZhen" sender:_askId];
    } else if ([_type intValue] == 1) {
        [self performSegueWithIdentifier:@"toOnline" sender:_askId];
    } else if ([_type intValue] == 2) {
        [self performSegueWithIdentifier:@"toTel" sender:_askId];
    } else if ([_type intValue] == 3) {
        [self performSegueWithIdentifier:@"toClinic" sender:_askId];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"toYiZhen"]) {
        
        QuestionDetailController *detail = [segue destinationViewController];
        detail.askId = sender;
        detail.ifDownload = _i;
        
    } else if ([segue.identifier isEqualToString:@"toOnline"]) {
        
        QuestionOnlineDetailController *onLine = [segue destinationViewController];
        onLine.askId = sender;
        onLine.ifDownload = _i;
        
    } else if ([segue.identifier isEqualToString:@"toTel"]) {
        
        QuestionTelDetailController *tel = [segue destinationViewController];
        tel.askId = sender;
        tel.ifDownload = _i;
        
    } else if ([segue.identifier isEqualToString:@"toClinic"]) {
        
        ClinicDetailViewController *clinic = [segue destinationViewController];
        clinic.askId = sender;
        clinic.ifDownload = _i;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"推荐治疗方案"];
    
    if (!_i || _i == 5) {
        self.navigationItem.rightBarButtonItem = [AppUtils generateNavigationBarButtonWithTitle:@"详情" withTarget:self action:@selector(goDetail)];
    }
    
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
    _iconView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    _customerAge.text=self.user_age;
    _customerName.text=self.user_name;
    _customerSex.text=self.user_sex;
    
    [_iconImageVIew   setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecm-user.oss-cn-beijing.aliyuncs.com/image/%@",self.user_icon]] placeholderImage:[UIImage imageNamed:@"icon"]];
    //获取scorllView高度
  DetailtreatMents *detailm=[self.dataAry firstObject];
    _diagnosis.text=detailm.ask_diagnose;
    
  backviewHeight.constant= [self labelwidthWith:detailm.ask_remark]+[self labelwidthWith:detailm.ask_diagnose]+330+detailm.cureAry.count*21;
    _dataAry=[NSArray arrayWithArray:detailm.cureAry];
    NSLog(@"--------------%@",detailm.ask_diagnose);
    
    _dtScv.contentSize=CGSizeMake(kScreenWidth, backviewHeight.constant+30);
  
    // bringSubviewToFront:<#(nonnull UIView *)#>
    
    NSArray *curetypeAry=@[@"自费",@"医保"];
    CureSchemeChildInfo *cureInfo1=[detailm.cureAry firstObject];
    NSString *str=[NSString stringWithFormat:@"%@%@*%@",cureInfo1.drug_type,curetypeAry[cureInfo1.durg_ishealthcare.intValue],cureInfo1.all_num];
    
    _medicineTv.rowHeight=21;
    medicinetvHeight.constant=detailm.cureAry.count*21;
    _medicineTv.separatorStyle=UITableViewCellSeparatorStyleNone;

   
    if(detailm.cureAry.count==0&&!cureInfo1.drug_type)
         _medicinel.text=nil;
    else 
    _medicinel.text=str;
    
    _remarks.text=detailm.ask_remark;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  
    return _dataAry.count;

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    MedicineTableViewCell *medicineCell=[tableView dequeueReusableCellWithIdentifier:@"medicinecell" forIndexPath:indexPath];
    CureSchemeChildInfo *cureInfo=_dataAry[indexPath.row];
    medicineCell.medicinel.text=cureInfo.drug_name;
    medicineCell.numl.text=[NSString stringWithFormat:@"%@袋",cureInfo.drug_num];
    medicineCell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell=medicineCell;
    return cell;
}
-(CGFloat)labelwidthWith:(NSString *)str
{
    
    UIFont *font= [UIFont systemFontOfSize:16.0];
    NSDictionary *fontDic = @{NSFontAttributeName:font};
    
    CGRect labelRect = [str boundingRectWithSize:CGSizeMake(kScreenWidth-70,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    CGSize size = labelRect.size;
    return size.height;
}

-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
